package top.hcode.hoj.controller.admin;


import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.hcode.hoj.common.result.CommonResult;
import top.hcode.hoj.pojo.vo.ConfigurationVO;
import top.hcode.hoj.service.admin.system.ConfigurationService;

import java.util.List;

/**
 * 配置项信息
 *
 *
 */

@RestController
@RequestMapping("/api/admin/configuration")
public class ConfigurationController{

    @Autowired
    private ConfigurationService configurationService;


    /**
     * 获取所有的父级节点
     * */

    @GetMapping("/get-all-parents")
    @RequiresRoles(value = {"root", "admin",}, logical = Logical.OR)
    @RequiresAuthentication
    public CommonResult<List<ConfigurationVO>> getAllParents(){
        ConfigurationVO configurationVO = new ConfigurationVO();
        configurationVO.setParentCode("-1");//所有父级节点的父级节点都是-1'
        List<ConfigurationVO> configurationVOList = configurationService.quaryAll(configurationVO);
        return  CommonResult.successResponse(configurationVOList);
    }

    /**
     * 根据code查询配置项
     */
    @GetMapping("/get-configuration-code")
    @RequiresAuthentication
    public CommonResult<List<ConfigurationVO>> quaryByCode(@RequestParam("code") String code){
        ConfigurationVO configurationVO = new ConfigurationVO();
        configurationVO.setCode(code);
        List<ConfigurationVO> configurationVOList = configurationService.quaryAll(configurationVO);
        return  CommonResult.successResponse(configurationVOList);
    }


    /**
     * 根据父级code查询父级下面的所有配置项
     */
    @GetMapping("/get-configuration-parent-code")
    @RequiresAuthentication
    public CommonResult<List<ConfigurationVO>> quaryChildByCode(@RequestParam("code") String code){
        ConfigurationVO configurationVO = new ConfigurationVO();
        configurationVO.setParentCode(code);
        List<ConfigurationVO> configurationVOList = configurationService.quaryAll(configurationVO);
        return  CommonResult.successResponse(configurationVOList);
    }
    /**
     *批量插入操作
     */
    @PostMapping("/batchInsert")
    @RequiresRoles(value = {"root", "admin",}, logical = Logical.OR)
    @RequiresAuthentication
    public CommonResult batchInsert(@RequestBody List<ConfigurationVO> configList) {
        try {
            int count = configurationService.batchInsert(configList);
            return CommonResult.successResponse(count);
        } catch (Exception e) {
            return CommonResult.errorResponse("批量插入失败");
        }
    }

    @PutMapping("/updateByCode")
    @RequiresRoles(value = {"root", "admin",}, logical = Logical.OR)
    @RequiresAuthentication
    public CommonResult updateBycode(@RequestBody ConfigurationVO configurationVO) {
        try {
            if(configurationVO.getCode()!=null){
                return CommonResult.successResponse( configurationService.updateByCode(configurationVO));
            }else {
                return CommonResult.errorResponse("没有code");
            }
        }catch (Exception e) {
            return CommonResult.errorResponse("更新失败");
        }
    }


}